﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Collections;

namespace LanguageStudy
{
    public partial class LSForm : Form
    {
        WordsListView m_wlv = new WordsListView();

        public LSForm()
        {
            InitializeComponent();
        }

        private void LSForm_Load(object sender, EventArgs e)
        {
            this.Size = new Size(800, 600);

            this.gbWords.Controls.Add(this.m_wlv);
            m_wlv.Dock = DockStyle.Fill;

            AddGroup("默认");
            this.lbGroup.SelectedIndex = 0;

            this.notifyIcon.Icon = this.Icon;
        }

        private void LSForm_SizeChanged(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized)
            {
                foreach (Form frm in Application.OpenForms)
                {
                    if (frm.Name == "WSForm")
                    {
                        HideToNotity();
                        return;
                    }
                }
            }
        }

        /// 分组功能
        private void AddGroup(string strGroupName)
        {
            WordsHandle _whNew = new WordsHandle(strGroupName);
            this.lbGroup.Items.Add(_whNew);
        }

        private void UpdateGroup(int nID)
        {
            this.lbGroup.Items[nID] = this.lbGroup.Items[nID];

            if (nID == this.lbGroup.SelectedIndex)
                m_wlv.SetWords(((WordsHandle)this.lbGroup.SelectedItem).GetWords());
        }

        private int m_nCurGroupID = -1;
        private void lbGroup_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.lbGroup.SelectedItem == null)
                return;

            if (this.lbGroup.SelectedIndex == m_nCurGroupID)
                return;

            m_nCurGroupID = this.lbGroup.SelectedIndex;
            UpdateGroup(this.lbGroup.SelectedIndex);
        }

        /// 菜单功能
        private void tsmiGroupAddNew_Click(object sender, EventArgs e)
        {
            AddGroup("新分组");
        }

        private void tsmiGroupRename_Click(object sender, EventArgs e)
        {
            if (this.lbGroup.SelectedItem == null)
                return;

            string _strNewName = InputBox("分组重命名", "分组重命名",
                ((WordsHandle)this.lbGroup.SelectedItem).Name);

            if (_strNewName != "")
            {
                ((WordsHandle)this.lbGroup.SelectedItem).Name = _strNewName;
                UpdateGroup(this.lbGroup.SelectedIndex);
            }
        }

        private void tsmiGroupDelete_Click(object sender, EventArgs e)
        {
            if (this.lbGroup.SelectedItem == null)
                return;

            this.lbGroup.Items.RemoveAt(this.lbGroup.SelectedIndex);
            this.m_wlv.Clear();
        }

        /// 按钮功能
        private string m_strDirectory = "c:\\";
        private void btnImport_Click(object sender, EventArgs e)
        {
            if (this.lbGroup.SelectedItem == null)
                return;

            OpenFileDialog _dlgOpenFile = new OpenFileDialog();

            _dlgOpenFile.InitialDirectory = m_strDirectory;
            _dlgOpenFile.Filter = "有道导出文件 (*.txt)|*.txt|所有文件 (*.*)|*.*";
            _dlgOpenFile.FilterIndex = 1;
            _dlgOpenFile.RestoreDirectory = true;

            if (_dlgOpenFile.ShowDialog() == DialogResult.OK)
            {
                // 路径处理
                m_strDirectory = _dlgOpenFile.FileName;

                // 导入文件
                ArrayList _alWords = ((WordsHandle)this.lbGroup.SelectedItem).ImportFile(_dlgOpenFile.FileName);
                if (_alWords == null)
                    return;

                // 更新组内容
                UpdateGroup(this.lbGroup.SelectedIndex);
            }
        }

        private void btnBuild_Click(object sender, EventArgs e)
        {
            if (this.lbGroup.SelectedItem == null)
                return;

            ArrayList _alWords = ((WordsHandle)this.lbGroup.SelectedItem).RandomWords(0);

            if (_alWords.Count == 0)
                return;

            OPForm _opf = new OPForm(_alWords);
            _opf.ShowDialog();
        }

        private WSForm m_frmWS = null;
        private void btnShow_Click(object sender, EventArgs e)
        {
            if (((WordsHandle)this.lbGroup.SelectedItem).GetWords().Count <= 0)
                return;

            foreach (Form frm in Application.OpenForms)
            {
                if (frm.Name == "WSForm")
                {
                    return;
                }
            }

            m_frmWS = new WSForm(((WordsHandle)lbGroup.SelectedItem));
            m_frmWS.Show();
            HideToNotity();
        }

        private void btnQuit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void notifyIcon_Click(object sender, EventArgs e)
        {
            this.Visible = true;
            this.WindowState = FormWindowState.Normal;
            this.notifyIcon.Visible = false;
        }

        private void HideToNotity()
        {
            this.Hide();
            this.notifyIcon.Visible = true;
        }


        private string InputBox(string Caption, string Hint, string Default)
        {   
            Form InputForm = new Form();
            InputForm.MinimizeBox = false;
            InputForm.MaximizeBox = false;
            InputForm.StartPosition = FormStartPosition.CenterScreen;
            InputForm.Width = 220;
            InputForm.Height = 150;
            //InputForm.Font.Name = "宋体";
            //InputForm.Font.Size = 10;
 
            InputForm.Text = Caption;
             
            Label lbl = new Label();
            lbl.Text = Hint;
            lbl.Left = 10;
            lbl.Top = 20;
            lbl.Parent = InputForm;
            lbl.AutoSize = true;
            
            TextBox tb = new TextBox();
            tb.Left = 30;
            tb.Top = 45;
            tb.Width = 160;
            tb.Parent = InputForm;
            tb.Text = Default;
            tb.SelectAll();
             
            Button btnok = new Button();
            btnok.Left = 30;
            btnok.Top = 80;
            btnok.Parent = InputForm;
            btnok.Text = "确定";
            InputForm.AcceptButton = btnok;//回车响应
 
             btnok.DialogResult = DialogResult.OK;
             
            Button btncancal = new Button();
            btncancal.Left = 120;
            btncancal.Top = 80;
            btncancal.Parent = InputForm;
            btncancal.Text = "取消";
            btncancal.DialogResult = DialogResult.Cancel;
            try
            {
                if (InputForm.ShowDialog() == DialogResult.OK)
                {
                    return tb.Text;
                }
                else
                {
                    return "";
                }
            }
            finally
            {
                InputForm.Dispose();
            }
        }
    }
}